package cn.bincker.mybatis.encrypt.converter.impl;

import cn.bincker.mybatis.encrypt.exception.InvalidDataException;

import java.nio.ByteBuffer;

public class DoubleEncryptConverter extends BaseEncryptConverter<Double> {
    @Override
    public Double convertNonNull(byte[] data) {
        var byteBuffer = ByteBuffer.wrap(data);
        if (data.length >= Double.BYTES) {
            return byteBuffer.getDouble();

        } else if (data.length >= Float.BYTES){
            return (double) byteBuffer.getFloat();
        }
        throw new InvalidDataException("double at least need " + Float.BYTES + " bytes but got " + data.length);
    }

    @Override
    public byte[] convertNonNull(Double object) {
        byte[] result = new byte[Double.BYTES];
        ByteBuffer.wrap(result).putDouble(object);
        return result;
    }
}
